package com.tetris;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;

// ██
// ██
class Block1 extends Block {
    /**
     * 构造方法。
     */
    public Block1() {
        mBlocks[1][1].mUsed = mBlocks[1][2].mUsed = mBlocks[2][1].mUsed = mBlocks[2][2].mUsed = true;
    }

    /**
     * 进入预览区域。
     * @param preview   预览区域
     * @param drawer    绘图者
     */
    public void enterPreview(View preview, Canvas drawer) {
        final int x = preview.getWidth() / 2 - Const.GRID_SIZE;
        final int y = preview.getHeight() / 2 - Const.GRID_SIZE;

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                mPaint.setColor(mColor);
                drawer.drawRect(x + Const.GRID_SIZE * j, y + Const.GRID_SIZE * i,
                    x + Const.GRID_SIZE * (j + 1), y + Const.GRID_SIZE * (i + 1), mPaint);
                Block.drawEdge(drawer, x + Const.GRID_SIZE * j, y + Const.GRID_SIZE * i, mPaint);
            }
        }
    }

    /**
     * 擦除。
     */
    protected void erase() {
        final Rect rect = new Rect(Const.GRID_SIZE * (mLine + 1), Const.GRID_SIZE * (mRow + 1),
            Const.GRID_SIZE * (mLine + 3), Const.GRID_SIZE * (mRow + 3));

        super.eraseRect(rect);
    }

    /**
     * 获取底端所在的行数。底端是指方块最下方的有效格。
     * @return 底端所在的行数
     */
    public int getBottom() {
        return mRow + 2;
    }

    /**
     * 获取顶端所在的行数。顶端是指方块最上方的有效格。
     * @return 顶端所在的行数
     */
    public int getTop() {
        return mRow + 1;
    }

    /**
     * 对不能下落的处理。
     */
    protected void handleCannotMoveDown() {
        Game.sExistentBlocks[mRow + 1][mLine + 1].mUsed =
        Game.sExistentBlocks[mRow + 1][mLine + 2].mUsed =
        Game.sExistentBlocks[mRow + 2][mLine + 1].mUsed =
        Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed = true;
        Game.sExistentBlocks[mRow + 1][mLine + 1].mColor =
        Game.sExistentBlocks[mRow + 1][mLine + 2].mColor =
        Game.sExistentBlocks[mRow + 2][mLine + 1].mColor =
        Game.sExistentBlocks[mRow + 2][mLine + 2].mColor = mColor;
    }

    /**
     * 下落。
     * @return 成功则返回 true，否则返回 false。
     */
    public boolean moveDown() {
        if (mRow == 17) {
            this.handleCannotMoveDown();
            return false;
        }
        if (Game.sExistentBlocks[mRow + 3][mLine + 1].mUsed ||
            Game.sExistentBlocks[mRow + 3][mLine + 2].mUsed) {
            this.handleCannotMoveDown();
            return false;
        }

        return super.moveDown();
    }

    /**
     * 左移。
     */
    public void moveLeft() {
        if (mLine == -1) {
            return;
        }
        if (Game.sExistentBlocks[mRow + 1][mLine].mUsed ||
            Game.sExistentBlocks[mRow + 2][mLine].mUsed) {
            return;
        }

        super.moveLeft();
    }

    /**
     * 右移。
     */
    public void moveRight() {
        if (mLine == 7) {
            return;
        }
        if (Game.sExistentBlocks[mRow + 1][mLine + 3].mUsed ||
            Game.sExistentBlocks[mRow + 2][mLine + 3].mUsed) {
            return;
        }

        super.moveRight();
    }

    /**
     * 变形。
     */
    public void transform() {
    }
}
